<?php
namespace Plu\Library\Services\Redis\Blacklist;

use Plu\Library\Services\ServiceBase;

class BlacklistRedis extends ServiceBase {
    const KEY_OF_Blacklist = "r:%s:blacklist";

    public function hasBlocked($roomId, $userId) {
        $blockInAll = $this->getBlockTime(0, $userId);//全局黑名单
        $blockInRoom = 0;//房间黑名单
        if ($roomId > 0) {
            $blockInRoom = $this->getBlockTime($roomId, $userId);
        }

        return max(array($blockInAll, $blockInRoom));
    }


    public function getBlockTime($roomId, $userId) {
        $blockkey = sprintf(self::KEY_OF_Blacklist, $roomId);

        $redis = $this->getRedis('blacklist');

        $ticks = $redis->hGet($blockkey, $userId);
        $value = floor(($ticks - 621355968000000000) / 10000000);

        if ($value > time()) {
            return $value;
        }

        return 0;
    }
}
?>
